#include "DataRepo.hpp"
#include "Message.hpp"
#include <boost/asio.hpp>


using boost::asio::ip::udp;

class Connection_End_Client_UDP
{
public:

		typedef std::map< int, MsgPointer > ID_MSGPOINTER;

		Connectiontype	m_Entity;
		int				m_CnnectionID;
		int				m_LastMessageID;
		DataRepo&		m_dataRepo;
		udp::endpoint	remote_endpoint_;
		boost::asio::io_service& m_io_service_;
		//udp::socket 	m_udpSock;
		DataType		m_BindType;
		std::string		m_ClientName;
		ID_MSGPOINTER	m_NonAckMessages;
		bool			m_bNotResponding;



	typedef boost::shared_ptr<Connection_End_Client_UDP> ConnectionEnd_Client_Pointer;

	//boost::asio::io_service& io_service ,
	Connection_End_Client_UDP( int connectionID, DataRepo& repo , udp::endpoint& ep , boost::asio::io_service& io , std::string Name ): m_dataRepo( repo ),remote_endpoint_( ep ),m_io_service_( io )
	{

		m_CnnectionID = connectionID;
		std::cout<<"Instantiating a new instance of Connection_end with ID"<< m_CnnectionID <<std::endl;
		m_Entity = Unknown;
		m_LastMessageID = 0 ;
		m_BindType = ( DataType )(rand()%2);
		m_ClientName = Name;
		m_bNotResponding = false;
		std::cout<<"Leaving constructor \n";
	}

	void SendData( MsgPointer msg )
	{
		std::string mm = msg->Encode();
		udp::socket sock( m_io_service_ );
		sock.connect( remote_endpoint_ );
		sock.send_to( boost::asio::buffer( mm.c_str(), mm.length() ) , remote_endpoint_ );
		sock.close();
	}

//void Send( Message& msg )
//	{
//		std::cout<<"Connection_End::Sending data to remote point\n";
//		udp::socket sock( m_io_service_ ) ;
//		std::string mm = msg.Encode();
//		sock.send_to( boost::asio::buffer( mm.c_str(), mm.length() ) ,remote_endpoint_ );
//	}


};
